Page({
  data: { 
    // activitiy: [
    //   {
    //     id: 1,
    //     name: "网球陪练4.0",
    //     date: "5月15日 07:00-23:22",
    //     location: "静安寺 距您1km",
    //     participants: "已报名3人 | 差2人",
    //     price: "￥120.00",
    //     status: "报名中"
    //   }
    // ]
  },
  onSubmit: function(e) {
    const isLoggedIn = wx.getStorageSync('isLoggedIn');
    if (!isLoggedIn) {
      wx.setStorageSync('redirectUrl', '/pages/create/create'); // 保存当前页面 URL
      wx.showModal({
        title: '未登录',
        content: '请先登录后再提交活动。',
        showCancel: false,
        success: () => {
          wx.navigateTo({
            url: '/pages/login/login',
          });
        }
      });
      return;
    }

    // 获取表单数据
    const form = e.detail.value;
    // 构造新活动对象
    const newActivity = {
      id: Date.now(),
      name: form.name, // Match 'name' field in index.js
      date: form.date, // Match 'date' field in index.js
      location: form.location, // Match 'location' field in index.js
      participants: `已报名0人 | 差${form.recruitNumber}人`, // Match 'participants' field in index.js
      price: `￥${parseFloat(form.price).toFixed(2)}`, // Match 'price' field in index.js
      status: "报名中" // Match 'status' field in index.js
    };
    // 存储到本地缓存
    wx.setStorageSync('newActivity', newActivity);
    wx.showModal({
      title: '提交成功',
      content: 'tennis booking activity',
      showCancel: false,
      success: () => {
        wx.switchTab({
          url: '/pages/index/index',
        });
      }
    });
  },
  onLoad: function() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().init && this.getTabBar().init();
    }
  }
});